import java.util.Arrays;

public class Solution1984 {
    public int minimumDifference(int[] nums, int k) {
        Arrays.sort(nums);
        int ans=Integer.MAX_VALUE;
        for(int i=0,j=i+k-1;j<nums.length;i++,j++){
            ans=Math.min(ans,nums[j]-nums[i]);
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution1984().minimumDifference(new int[]{9,4,1,7},2));
    }
}
